package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    /**
     * 查询用户列表
     * @param page 页码
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public PageBeanVo findUserList(Integer page, Integer pageSize) {
        IPage iPage = new Page(page,pageSize);
        iPage = userInfoMapper.selectPage(iPage,null);
        return new PageBeanVo(page,pageSize,iPage.getTotal(),iPage.getRecords());
    }
    @Override
    public PageBeanVo findUserInfo(Integer pageNumber, Integer pageSize) {

       /* Long count = (long)userInfoMapper.selectCount (new QueryWrapper<> ());
        List<UserInfo> userInfos = userInfoMapper.selectList (new QueryWrapper<> ());
        return new PageBeanVo (pageNumber, pageSize, count, userInfos);*/

        IPage page = new Page (pageNumber , pageSize);
        IPage iPage = userInfoMapper.selectPage (page, null);
        PageBeanVo pageBeanVo = new PageBeanVo (pageNumber, pageSize, iPage.getTotal (), iPage.getRecords ());
        return pageBeanVo;
    }

    @Override
    public List<UserInfo> findAll() {
        List<UserInfo> userInfos = userInfoMapper.selectList(null);
        return userInfos;
    }
}
